﻿var TechTypeValidationClientSideHide = false;
var PageNumber = 1;
var IsSearchView = false;
var TechTypeIDView = 0;
var IsDeleteView = false;
var TechDetailComElem = null;
var TechDetailComElemVal = null;
var TechDetailTimerId = null;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("SetupTechType") != -1) {
        $.validator.addMethod("SetupTechTypeRequireds", $.validator.methods.required, Messages["SetupTechTypeRequireds"]);
        $.validator.addMethod("SetupTechTypeMaxLength50", $.validator.methods.maxlength, Messages["SetupTechTypeMaxLength50"]);
        $.validator.addMethod("SetupTechTypeMaxLength120", $.validator.methods.maxlength, Messages["SetupTechTypeMaxLength120"]);
        $.validator.addClassRules({
            Required: {
                SetupTechTypeRequireds: true,
            },
            MaxLength50: {
                SetupTechTypeMaxLength50: 50,
            },
            MaxLength120: {
                SetupTechTypeMaxLength120: 120,
            },
        });

        $('#LoadTechTypeDetailsForm').validate();
        LoadTechTypeGrid();
    }
});
$(document).on('change', '.ChangedTechTypeElements', function () {
    $('#TechTypeDetailsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '.TechTypeAddElementChange', function () {
    $('#TechTypeAddBtn').addClass('BackColorRed');
});
function DeleteTechType(TechTypeID) {
    jConfirm('Are you sure you want to delete this Tech Type?', 'Warning!', function (retVal) {
        if (retVal == true) {
            TechTypeIDView = TechTypeID;
            IsDeleteView = true;
            LoadTechTypeGrid();
        }
    });
}
function AddNewTechType() {
    TechTypeValidationClientSideHide = false;
    $.ajax({
        url: urls["SetupAddNewTechType"],
        type: 'GET',
        success: function (data) {
            $("#AddNewTechTypeDiv").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            IsDeleteView = false;
            AddNewTechTypePopUp("AddNewTechTypeDiv");
        }
    });
}
$(document).on('click', '#TechTypeMoreBtn', function () {
    PageNumber = PageNumber + 1;
    IsDeleteView = false;
    LoadTechTypeGrid();
});
$(document).on('click', '#TechTypeSearchBtn', function () {
    IsSearchView = true;
    IsDeleteView = false;
    LoadTechTypeGrid();
});
function AddNewTechTypePopUp(PopUpDiv) {
    $.colorbox({
        width: "600px",
        height: "245px",
        inline: true,
        href: "#" + PopUpDiv,
        onComplete: function () {
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $("#" + PopUpDiv).hide();
        }
    });
}
$(document).on('click', '#TechTypeAddBtn', function () {
    var $form = $('#AddNewTechTypeForm');
    if ($form.valid()) {
        TechTypeValidationClientSideHide = false;
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: $form.serialize(),
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            },
            success: function (data) {
                if (data.indexOf("Error") != -1)
                    jAlert(data, "Error");
            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#TechTypeAddBtn').removeClass('BackColorRed')
                jAlert("Data SuccessFully Saved:", "Successful");
                $('#cboxClose').click();
                IsDeleteView = false;
                LoadTechTypeGrid();
            }
        });
    }
    else
        TechTypeValidationClientSideHide = true;
});

$(document).on('click', '#TechTypeDetailsSaveBtn', function () {
    var $form = $('#LoadTechTypeDetailsForm');
    if ($form.valid()) {
        if ($form[0].length > 0) {
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#TechTypeDetailsSaveBtn').removeClass('BackColorRed')
                    jAlert("Data SuccessFully Saved:", "Successful");
                    IsDeleteView = false;
                    LoadTechTypeGrid();
                }
            });
        }
        else
            jAlert("You have not data to save:", "Attention!");
    }
});
function LoadTechTypeGrid() {
    var SetupTechTypesGridViewModel = {
        TechTypeCodeText: $("#TechTypeCodeText").val(),
        TechTypeDescriptionText: $("#TechTypeDescriptionText").val(),
        PageNum: parseInt(PageNumber),
        IsSearch: IsSearchView,
        IsDelete: IsDeleteView,
        TechTypeID: parseInt(TechTypeIDView),
        IsActiveSearch: $("#IsActiveSearch").val(),
    };

    var DTO = JSON.stringify(SetupTechTypesGridViewModel);
    $.ajax({
        url: urls["SetupLoadTechTypesGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#SetupTechTypeGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            IsDeleteView = false;
        }
    });
}
// text element complete text 
$(document).on('input', '.QuotaTxtComplete', function () {
    if (TechDetailTimerId == null) {
        TechDetailComElem = $(this).attr('id');
        TechDetailComElemVal = $("#" + TechDetailComElem).val();
        TechDetailTimerId = setInterval(function () {

            var keyword = $("#" + TechDetailComElem).val();
            if (keyword == TechDetailComElemVal) {
                var PropName = "";
                if (TechDetailComElem.indexOf("TechTypeCodeText") != -1)
                    PropName = "TechTypeCodeText";
                if (TechDetailComElem.indexOf("TechTypeDescriptionText") != -1)
                    PropName = "TechTypeDescriptionText";

                var position = $("#" + TechDetailComElem).offset();
                position.top = position.top + $("#" + TechDetailComElem).height() + 5;
                $.ajax({
                    url: urls["SetupAutoComplete"],
                    data: { 'PropName': PropName, 'keyword': keyword },
                    type: 'POST',
                    success: function (data) {
                        $("#AutoCompleteDiv").html(data);
                        var width = $("#" + TechDetailComElem).css("width");
                        $("#AutoCompleteDiv").css("width", width);
                        $("#AutoCompleteDiv").show();
                        $("#AutoCompleteDiv").offset(position);
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        clearInterval(TechDetailTimerId);
                        TechDetailTimerId = null;
                    }
                });
            }
            else
                TechDetailComElemVal = keyword;
        }, 600);
    }
});
$(document).on('click', '.QuotaAutoCompleteDivClose', function () {
    $(this).parent().parent().hide();
});
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function AutoCompleteRowClick(key, val) {
    val = htmlDecode(val);
    $("#" + TechDetailComElem).val(val);
    if (TechDetailComElem.indexOf("TechTypeCodeText") != -1)
        $("#" + TechDetailComElem).next().val(key);
    if (TechDetailComElem.indexOf("TechTypeDescriptionText") != -1)
        $("#" + TechDetailComElem).next().val(key);
    $("#AutoCompleteDiv").hide();
}


var originalClose = $.colorbox.close;
$.colorbox.close = function () {
    if ($('#TechTypeAddBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ClosePopUp"], Messages["ClosePopUpTitle"], function (retVal) {
            if (retVal == true) {
                $("#AddNewTechTypeForm").empty();
                LoadTechTypeGrid();
                originalClose();
            }
        });
    }
    else {
        $("#AddNewTechTypeForm").empty();
        LoadTechTypeGrid();
        originalClose();
    }
};
$(document).on('change', '.ElementsForValidation', function () {
    if ($(this).valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
    if (TechTypeValidationClientSideHide) {
        if ($(this).closest("form").valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
    }
    else if ($(this).valid()) {
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            $(this).valid();
        });
    }
});
$(document).on('change', '.ForValidation', function () {
    if ($(this).valid)
        $(this).removeClass('table_inputs_Unvalidated');
    if ($(this).closest("form").valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            if ($(this).valid())
                $(this).removeClass('table_inputs_Unvalidated');
        });
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
});